
#ifndef __M_UTIL_H__
#define __M_UTIL_H__
#include "log.hpp"
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <memory>
#include <mysql/mysql.h>
#include <jsoncpp/json/json.h>
// 工具类的实现

class mysql_util
{
public:
    static MYSQL *mysql_create(const std::string &host,
                               const std::string &username,
                               const std::string &passwd,
                               const std::string &dbname,
                               uint16_t port = 3306)
    {
        // 初始化mysql操作句柄
        MYSQL *mysql = mysql_init(nullptr);
        if (mysql == nullptr)
        {
            ELOG("初始化mysql操作句柄失败");
            return nullptr;
        }
        // 连接服务器并打开数据库
        mysql_real_connect(mysql, host.c_str(), username.c_str(),
                           passwd.c_str(), dbname.c_str(), port, nullptr, 0);
        // 设置客户端字符集
        int ret;
        ret = mysql_set_character_set(mysql, "utf8");
        if (ret != 0)
        {
            ELOG("设置mysql客户端字符集失败");
            mysql_close(mysql);
            return nullptr;
        }
        return mysql;
    }
    static bool mysql_exec(MYSQL *mysql, const std::string &sql)
    {
        int ret = mysql_query(mysql, sql.c_str());
        if (ret != 0)
        {
            ELOG("%s", sql.c_str());
            ELOG("语句执行失败,错误信息: %s", mysql_error(mysql));
            return false;
        }
        return true;
    }
    static void mysql_destory(MYSQL *mysql)
    {
        if (mysql != nullptr)
            mysql_close(mysql);
    }
};

class Json_util
{
public:
    static bool seralize(const Json::Value &root, std::string &str)
    {
        // Json::StreamWriterBuilder writer;
        // out = Json::writeString(writer, val);
        // return true;
        Json::StreamWriterBuilder swb;
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        std::stringstream ss;
        int ret = sw->write(root, &ss); // ##################1注意这里不能加&,发现了一个bug
        if (ret != 0)
        {
            ELOG("序列化失败");
            return false;
        }
        str = ss.str();
        return true;
        // Json::StreamWriterBuilder swb;
        // Json::StreamWriter* sw=swb.newStreamWriter();
        // std::stringstream ss;
        // int ret=sw->write(&root,&ss);
        // if(ret!=0)
        // {
        //     ELOG("序列化失败");
        //     return false;
        // }
        // str=ss.str();
        // delete sw;
        // return true;
    }
    static bool parse(const std::string &str, Json::Value &root)
    {
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        int ret = cr->parse(str.c_str(), str.c_str() + str.size(), &root, nullptr);
        if (ret == 0)
        {
            ELOG("反序列化失败");
            return false;
        }
        return true;
        // Json::CharReaderBuilder crb;
        // Json::CharReader *cr = crb.newCharReader();
        // int ret = cr->parse(str.c_str(), str.c_str() + str.size(), &root, nullptr);
        // if (ret != 0)
        // {
        //     ELOG("反序列化失败");
        //     return false;
        // }
        // delete cr;
        // return true;
    }
};

class string_util
{
public:
    static int split(const std::string &str, const std::string &sep, std::vector<std::string> &res)
    {
        size_t pos, idx = 0;
        while (idx < str.size())
        {
            pos = str.find(sep, idx);
            if (pos == std::string::npos)
            {
                res.push_back(str.substr(idx));
                break;
            }
            if (idx == pos)
            {
                idx += sep.size();
                continue;
            }
            res.push_back(str.substr(idx, pos - idx));
            idx = pos + sep.size();
        }
        return res.size();
    }
};

class file_util
{
public:
    static bool read(const std::string &filename, std::string &body)
    {
        std::ifstream ifs(filename, std::ios::binary);
        if (ifs.is_open() == false)
        {
            ELOG("%s 文件打开失败", filename.c_str());
            return false;
        }
        size_t fsize = 0;
        ifs.seekg(0, std::ios::end);
        fsize = ifs.tellg();
        ifs.seekg(0, std::ios::beg);

        std::vector<char> buffer(fsize);
        ifs.read(buffer.data(), fsize);
        if (ifs.good() == false)
        {
            ELOG("%s 文件读取失败", filename.c_str());
            ifs.close();
            return false;
        }
        std::string out(buffer.begin(), buffer.end());
        body = out;

        ifs.close();
        return true;
        // int filesize=Filesize(filename);
        // if(filesize>0)
        // {
        //     std::ifstream in(filename);
        //     if(!in.is_open())
        //     return false;
        //     body.resize(filesize);
        //     in.read((char*)body.c_str(),filesize);
        //     in.close();
        // }
        // else
        // {
        //     return false;
        // }
        // return true;
    }
    static int Filesize(const std::string &filename)
    {
        // 以字节流形式打开
        std::ifstream in(filename, std::ios::binary);
        if (!in.is_open())
            return -1;

        in.seekg(0, in.end);
        int filesize = in.tellg();
        in.seekg(0, in.beg);
        in.close();
        return filesize;
    }
};
#endif